
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
    use axum::{
        routing::{get, post},
        Router,
    };
    use leptos::prelude::*;
    use leptos_axum::{generate_route_list, LeptosRoutes};
    use shq_pas_ui::{adapters, app::{shell, App},watching_config};
    use tracing_appender::rolling::{RollingFileAppender,Rotation};
    use tracing_subscriber::{filter::LevelFilter, fmt::{self, time::FormatTime}, prelude::*};
    use std::time::SystemTime;
    use chrono::{Local, DateTime};

    // 自定义计时器，使用本地时间
    struct LocalTimer;

    impl FormatTime for LocalTimer {
        fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result {
            let now: DateTime<Local> = SystemTime::now().into();
            write!(w, "{}", now.format("%Y-%m-%d %H:%M:%S"))
        }
    }
    dotenv::dotenv().ok();
    let stdout_log = tracing_subscriber::fmt::layer()
        .with_timer(LocalTimer)
        .pretty();

    let general_log = RollingFileAppender::new(Rotation::DAILY, "./logs", "pas_general.log");
    let error_log = RollingFileAppender::new(Rotation::DAILY, "./logs", "pas_error.log");

    let general_layer = fmt::layer()
        .with_timer(LocalTimer)
        .with_writer(general_log)
        .with_filter(tracing_subscriber::filter::LevelFilter::INFO);

    let error_layer = fmt::layer()
        .with_timer(LocalTimer)
        .with_writer(error_log)
        .with_filter(tracing_subscriber::filter::LevelFilter::ERROR);

    tracing_subscriber::registry()
        .with(stdout_log.with_filter(LevelFilter::DEBUG))
        .with(general_layer)
        .with(error_layer)
        .init();
    shq_pas_backend::init().await;
    shq_excel_report::init_config().await.unwrap();

    let conf = get_configuration(None).unwrap();
    let leptos_options = conf.leptos_options;
    let addr = leptos_options.site_addr;
    let routes = generate_route_list(App);
    let app = Router::new()
        .route("/export_rept_xlsx", get(shq_pas_ui::adapters::export_rept_xlsx))
        .route("/binary_file/:id", get(shq_pas_ui::adapters::get_binary_file))
        .route("/export_xlsx_with_dao", get(shq_pas_ui::adapters::export_xlsx_from_page))
        .route("/export_import_home_visit_template/:uid", get(shq_pas_ui::adapters::export_import_home_visit_template))
        .route("/export_import_template/:conf_name", get(adapters::export_import_template))
        .route("/upload_excel_and_import", post(adapters::upload_excel_and_import))
        .route("/api/login", post(adapters::sys_login))
        .leptos_routes(&leptos_options, routes,{
            let leptos_options = leptos_options.clone();
            move || shell(leptos_options.clone())
        })
        .layer(axum::middleware::from_fn(shq_pas_ui::auth::auth_middleware))
        .fallback(leptos_axum::file_and_error_handler(shell))
        .with_state(leptos_options.clone());

    // 监控配置文件
    tokio::spawn(async {
        watching_config().await;
    });

    // run our app with hyper
    // `axum::Server` is a re-export of `hyper::Server`
    log::info!("listening on {}", addr);
    let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
    axum::serve(listener, app.into_make_service())
        .await
        .unwrap();

}

#[cfg(not(feature = "ssr"))]
pub fn main() {
    // no client-side main function
    // unless we want this to work with e.g., Trunk for a purely client-side app
    // see lib.rs for hydration function instead
}
